﻿using FluentNHibernate.Cfg.Db;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Reflection;
using YYSite.Application.Content.Articles;
using YYSite.Application.System.IdentityUsers;
using YYSite.Application.System.Roles;
using YYSite.Common.NHibernate;
using YYSite.Domain.Content.Articles;
using YYSite.Domain.System.IdentityUsers;
using YYSite.Domain.System.Roles;
using YYSite.Repository.Content.Articles;
using YYSite.Repository.System.IdentityUsers;
using YYSite.Repository.System.Roles;

namespace YYSite
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddDistributedMemoryCache();
         
            services.AddMvc();




            services.AddAuthorization();
            services.AddAntiforgery();
            services.AddLogging();
            services.AddOptions();
            services.AddWebEncoders();




            var assemblies = new[]{
                //Assembly.Load("YYSite.Domain"),
                Assembly.Load("YYSite.Repository")
            };
            var configuration=MsSqlConfiguration.MsSql2012.ConnectionString(s => s.Server(".").Username("sa").Password("123").Database("YYSite"));
            services.AddSingleton<INHFluentConfiguration>(new NHFluentConfiguration(configuration, assemblies));
            services.AddScoped<ITransactionManager, TransactionManager>();

        
            services.AddTransient<IIdentityUserRepository, IdentityUserRepository>();
            services.AddTransient<IIdentityUserService, IdentityUserService>();

            services.AddTransient<IRoleRepository, RoleRepository>();
            services.AddTransient<IRoleService, RoleService>();

            services.AddTransient<IArticleRepository, ArticleRepository>();
            services.AddTransient<IArticleService, ArticleService>();

            services.AddSingleton<YYSite.Common.Logging.ILogger>(new YYSite.Common.Logging.NullLogger());
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            
            loggerFactory.AddDebug();
        
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                //app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");

                routes.MapAreaRoute("System", "System", "System/{controller=Home}/{action=Index}");
            });
        }
    }


}
